<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>图片尺寸调整工具 - 在线工具集合</title>
    <script src="../utils/toast.js"></script>
    <link rel="stylesheet" href="../utils/styles.css">
    <style>
        @import url('https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600;700&display=swap');

        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Poppins', sans-serif;
            background: linear-gradient(135deg, #0f0c29, #302b63, #24243e);
            min-height: 100vh;
            color: #fff;
            position: relative;
            overflow-x: hidden;
        }

        .container {
            max-width: 1200px;
            margin: 0 auto;
            padding: 40px 20px;
        }

        .header {
            text-align: center;
            margin-bottom: 40px;
        }

        .header h1 {
            font-size: 2.5em;
            margin-bottom: 10px;
            background: linear-gradient(45deg, #00ff88, #00b4d8);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
        }

        .header p {
            color: rgba(255, 255, 255, 0.8);
            font-size: 1.1em;
        }

        .resizer-section {
            background: rgba(255, 255, 255, 0.05);
            border-radius: 24px;
            padding: 30px;
            margin-bottom: 30px;
            border: 1px solid rgba(255, 255, 255, 0.1);
            backdrop-filter: blur(10px);
        }

        .upload-area {
            border: 2px dashed rgba(255, 255, 255, 0.2);
            border-radius: 20px;
            padding: 40px;
            text-align: center;
            cursor: pointer;
            transition: all 0.3s ease;
            margin-bottom: 30px;
        }

        .upload-area:hover {
            border-color: #00ff88;
            background: rgba(255, 255, 255, 0.05);
        }

        .preview-container {
            position: relative;
            max-width: 800px;
            margin: 0 auto;
            overflow: hidden;
            background: rgba(0, 0, 0, 0.2);
            border-radius: 12px;
            margin-bottom: 30px;
        }

        .preview-container img {
            max-width: 100%;
            display: block;
            margin: 0 auto;
        }

        .size-options {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
            gap: 20px;
            margin: 30px 0;
            padding: 20px;
            background: rgba(255, 255, 255, 0.05);
            border-radius: 12px;
        }

        .option-group {
            display: flex;
            flex-direction: column;
            gap: 10px;
        }

        .option-group label {
            color: rgba(255, 255, 255, 0.9);
            font-size: 0.9em;
        }

        .option-group input {
            width: 100%;
            padding: 8px;
            background: rgba(255, 255, 255, 0.1);
            border: 1px solid rgba(255, 255, 255, 0.2);
            border-radius: 6px;
            color: #fff;
        }

        .option-group select {
            width: 100%;
            padding: 8px;
            background: rgba(255, 255, 255, 0.1);
            border: 1px solid rgba(255, 255, 255, 0.2);
            border-radius: 6px;
            color: #fff;
            cursor: pointer;
        }

        .option-group select option {
            background: #24243e;
            color: #fff;
        }

        .checkbox-group {
            display: flex;
            align-items: center;
            gap: 8px;
            margin-top: 10px;
        }

        .checkbox-group input[type="checkbox"] {
            width: auto;
        }

        .action-buttons {
            display: flex;
            gap: 20px;
            justify-content: center;
            margin-top: 30px;
        }

        .action-btn {
            padding: 15px 30px;
            border: none;
            border-radius: 12px;
            background: linear-gradient(45deg, #00ff88, #00b4d8);
            color: #1a1a1a;
            font-size: 1.1em;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s ease;
            box-shadow: 0 4px 15px rgba(0, 255, 136, 0.2);
        }

        .action-btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 8px 25px rgba(0, 255, 136, 0.3);
        }

        .back-link {
            display: inline-block;
            margin-bottom: 20px;
            color: #00ff88;
            text-decoration: none;
            font-weight: 500;
            transition: all 0.3s ease;
        }

        .back-link:hover {
            transform: translateX(-5px);
        }

        .image-info {
            text-align: center;
            margin: 20px 0;
            padding: 15px;
            background: rgba(255, 255, 255, 0.05);
            border-radius: 12px;
            font-size: 0.9em;
            color: rgba(255, 255, 255, 0.8);
        }

        @media (max-width: 768px) {
            .size-options {
                grid-template-columns: 1fr;
            }

            .action-buttons {
                flex-direction: column;
            }

            .action-btn {
                width: 100%;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <a href="../index.html" class="back-link">← 返回首页</a>
        
        <div class="header">
            <h1>图片尺寸调整工具</h1>
            <p>快速调整图片尺寸，支持等比缩放</p>
        </div>

        <div class="resizer-section">
            <div class="upload-area" id="uploadArea">
                <div class="upload-text">点击或拖拽图片到这里</div>
                <input type="file" id="imageInput" accept="image/*" style="display: none;">
            </div>

            <div class="preview-container">
                <img id="previewImage" style="display: none;">
            </div>

            <div class="image-info" id="imageInfo"></div>

            <div class="size-options">
                <div class="option-group">
                    <label>宽度 (px)</label>
                    <input type="number" id="widthInput" min="1">
                </div>
                <div class="option-group">
                    <label>高度 (px)</label>
                    <input type="number" id="heightInput" min="1">
                </div>
                <div class="option-group">
                    <label>预设尺寸</label>
                    <select id="presetSize">
                        <option value="custom">自定义</option>
                        <option value="1920x1080">1920 x 1080 (Full HD)</option>
                        <option value="1280x720">1280 x 720 (HD)</option>
                        <option value="800x600">800 x 600 (常用尺寸)</option>
                        <option value="640x480">640 x 480 (VGA)</option>
                    </select>
                </div>
                <div class="option-group">
                    <label>图片质量</label>
                    <input type="range" id="qualityInput" min="1" max="100" value="90">
                    <div class="checkbox-group">
                        <input type="checkbox" id="keepAspectRatio" checked>
                        <label for="keepAspectRatio">保持宽高比</label>
                    </div>
                </div>
            </div>

            <div class="action-buttons">
                <button class="action-btn" id="resizeBtn">调整尺寸</button>
                <button class="action-btn" id="resetBtn">重置图片</button>
                <button class="action-btn" id="downloadBtn">下载图片</button>
            </div>
        </div>
    </div>

    <script>
        const uploadArea = document.getElementById('uploadArea');
        const imageInput = document.getElementById('imageInput');
        const previewImage = document.getElementById('previewImage');
        const widthInput = document.getElementById('widthInput');
        const heightInput = document.getElementById('heightInput');
        const presetSize = document.getElementById('presetSize');
        const qualityInput = document.getElementById('qualityInput');
        const keepAspectRatio = document.getElementById('keepAspectRatio');
        const imageInfo = document.getElementById('imageInfo');

        let originalImage = null;
        let aspectRatio = 1;

        // 文件处理
        uploadArea.addEventListener('click', () => imageInput.click());
        uploadArea.addEventListener('dragover', (e) => {
            e.preventDefault();
            uploadArea.style.borderColor = '#00ff88';
        });
        uploadArea.addEventListener('dragleave', () => {
            uploadArea.style.borderColor = 'rgba(255, 255, 255, 0.2)';
        });
        uploadArea.addEventListener('drop', (e) => {
            e.preventDefault();
            const file = e.dataTransfer.files[0];
            if (file && file.type.startsWith('image/')) {
                handleImage(file);
            }
        });

        imageInput.addEventListener('change', (e) => {
            const file = e.target.files[0];
            if (file) {
                handleImage(file);
            }
        });

        function handleImage(file) {
            const reader = new FileReader();
            reader.onload = (e) => {
                originalImage = new Image();
                originalImage.onload = () => {
                    previewImage.src = originalImage.src;
                    previewImage.style.display = 'block';
                    aspectRatio = originalImage.width / originalImage.height;
                    
                    // 设置初始尺寸
                    widthInput.value = originalImage.width;
                    heightInput.value = originalImage.height;
                    
                    // 更新图片信息
                    updateImageInfo();
                };
                originalImage.src = e.target.result;
            };
            reader.readAsDataURL(file);
        }

        function updateImageInfo() {
            const size = Math.round((previewImage.src.length * 3/4) / 1024); // 估算文件大小
            imageInfo.textContent = `原始尺寸: ${originalImage.width} x ${originalImage.height} 像素 | 当前大小: 约 ${size} KB`;
        }

        // 预设尺寸处理
        presetSize.addEventListener('change', () => {
            if (presetSize.value === 'custom') return;
            
            const [width, height] = presetSize.value.split('x').map(Number);
            widthInput.value = width;
            heightInput.value = height;
        });

        // 尺寸输入处理
        widthInput.addEventListener('input', () => {
            if (keepAspectRatio.checked) {
                heightInput.value = Math.round(widthInput.value / aspectRatio);
            }
        });

        heightInput.addEventListener('input', () => {
            if (keepAspectRatio.checked) {
                widthInput.value = Math.round(heightInput.value * aspectRatio);
            }
        });

        // 调整尺寸功能
        document.getElementById('resizeBtn').addEventListener('click', () => {
            if (!originalImage) {
                toast.error('请先上传图片');
                return;
            }

            const canvas = document.createElement('canvas');
            const ctx = canvas.getContext('2d');
            
            canvas.width = parseInt(widthInput.value);
            canvas.height = parseInt(heightInput.value);
            
            // 绘制调整后的图片
            ctx.drawImage(originalImage, 0, 0, canvas.width, canvas.height);
            
            // 更新预览
            previewImage.src = canvas.toDataURL('image/jpeg', qualityInput.value / 100);
            updateImageInfo();
            toast.success('尺寸调整成功');
        });

        // 重置功能
        document.getElementById('resetBtn').addEventListener('click', () => {
            if (originalImage) {
                previewImage.src = originalImage.src;
                widthInput.value = originalImage.width;
                heightInput.value = originalImage.height;
                presetSize.value = 'custom';
                qualityInput.value = 90;
                updateImageInfo();
                toast.success('已重置');
            }
        });

        // 下载功能
        document.getElementById('downloadBtn').addEventListener('click', () => {
            if (!previewImage.src) {
                toast.error('请先上传图片');
                return;
            }

            const link = document.createElement('a');
            link.download = 'resized_image.jpg';
            link.href = previewImage.src;
            link.click();
            toast.success('开始下载');
        });
    </script>
</body>
</html> 